//
//  ST_ThumbUpPeculiarityBar.swift
//  btvideo
//
//  Created by mac on 2020/4/27.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import HandyJSON



/**
 点赞特质 表情 Bar
 */
class ST_ThumbUpPeculiarityBar: UIView {
    
    static let size = CGSize.init(width: 292, height: 56)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.shadowOffset = CGSize.init(width: 0, height: 0)
        self.shadowOpacity = 1
        self.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2)
        self.shadowRadius = 15
        
        self.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        let imageView0 = UIImageView()
        root_view.addSubview(imageView0)
        imageView0.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(self.gap)
            make.size.equalTo(self.itemSize)
        }
        
        let imageView1 = UIImageView()
        root_view.addSubview(imageView1)
        imageView1.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.right.equalTo(imageView0.snp.left).offset(self.gap)
            make.size.equalTo(self.itemSize)
        }
        
        let imageView2 = UIImageView()
        root_view.addSubview(imageView2)
        imageView2.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.right.equalTo(imageView1.snp.left).offset(self.gap)
            make.size.equalTo(self.itemSize)
        }
        
        let imageView3 = UIImageView()
        root_view.addSubview(imageView3)
        imageView3.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.right.equalTo(imageView2.snp.left).offset(self.gap)
            make.size.equalTo(self.itemSize)
        }
        
        imageViews = [imageView0,imageView1,imageView2,imageView3].reversed()
        
        imageViews.forEach { (item) in
            
            item.cornerRadius = itemSize.width / 2.0
            
            item.isUserInteractionEnabled = true
            
            let longGez = UILongPressGestureRecognizer.init(target: self, action: #selector(long_gez_handler(gez:)))
            
            item.addGestureRecognizer(longGez)
            
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(click_handler(gez:)))
            
            item.addGestureRecognizer(tap)
            
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Proeprties
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.init(white: 1, alpha: 0.95)
        control.cornerRadius = 28
        return control
    }()
    
    fileprivate var curr_long_gez:UILongPressGestureRecognizer?
    
    fileprivate let itemSize = CGSize.init(width: 36, height: 36)
    
    fileprivate let gap = -30.cgFloat
    
    fileprivate var imageViews = [UIImageView]()
    
    var model_list = [ST_ThumbUpBarModel](){
        didSet{
            
            for (idx,item) in model_list.enumerated(){
                
                imageViews[safe:idx]?.pin_setImage(from: item.value.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
                
            }
            
        }
    }
    
//    MARK: - Closure
    var start_lauch_block:((CGRect,ST_ThumbUpBarModel)->())?
    
    var stop_lauch_block:(()->())?
    
    var single_lauch_block:((CGRect,ST_ThumbUpBarModel)->())?
}


extension ST_ThumbUpPeculiarityBar{
    
    
    @objc func click_handler(gez:UILongPressGestureRecognizer){
        
        self.curr_long_gez?.isEnabled = false
        self.curr_long_gez?.isEnabled = true
        
        guard let currView = gez.view as? UIImageView , let idx = imageViews.firstIndex(of: currView),let model = model_list[safe:idx] else{
               return
           }
        
        self.single_lauch_block?(currView.frame,model)
    }
    
    @objc func long_gez_handler(gez:UILongPressGestureRecognizer){
            
        if gez.state == .began{
       
            self.curr_long_gez = gez
            
            guard let currView = gez.view as? UIImageView , let idx = imageViews.firstIndex(of: currView),let model = model_list[safe:idx] else{
                return
            }
            
            self.start_lauch_block?(currView.frame,model)
            
        }
        
        if gez.state != .began && gez.state != .changed{
            
            self.stop_lauch_block?()
        }
        
//        if gez.state == .changed{
//
//              print("emoji lauch----",Date())
//        }
        
        
//        print("emoji lauch----",Date())
    }
    
}



//MARK: - struct
struct ST_ThumbUpBarModel:HandyJSON,Hashable{
    var key = ""
    var value = ""
}
